package com.zqwzd.action;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.zqwzd.entity.Discounts;
import com.zqwzd.service.IDiscountsService;
import com.zqwzd.util.FileService;
import com.zqwzd.util.RandomTools;
import com.zqwzd.util.StringTools;

public class DiscountsAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	/** 商品图片 */
	private File imgFile;
	
	/** 商户LOGO */
	private File shopImgFile;
	
	private int start,limit;
	
	public String id;
	
	public String dataJson;
	
	public Discounts discounts;
	
	private IDiscountsService discountsService;
	
	public List<Discounts> discountList;
	
	/** 业务处理返回数据 */
	public Map<String, Object> data;
	
	public String addDiscounts(){
		String id = discounts.getId();
		if(!StringTools.isEmpty(id)){
			Map<String, Object> paramsM = new HashMap<String, Object>();
			paramsM.put("id", id);
			discountsService.deleteDiscounts(paramsM);
		}
		discounts.setId(RandomTools.randomStringUpper());
		String imgSuffix = discounts.getImgsrc().substring(discounts.getImgsrc().lastIndexOf(".")).toLowerCase();
		String imgName = RandomTools.randomStringUpper() + imgSuffix;
		FileService.saveFile(imgFile, imgName);
		String shopImgSuffix = discounts.getShopImgSrc().substring(discounts.getShopImgSrc().lastIndexOf(".")).toLowerCase();
		String shopImgName = RandomTools.randomStringUpper() + shopImgSuffix;
		FileService.saveFile(shopImgFile, shopImgName);
		discounts.setImgsrc(imgName);
		discounts.setShopImgSrc(shopImgName);
		discountsService.addDiscounts(discounts);
		convertData(discounts);
		return SUCCESS;
	}
	
	private void convertData(Discounts discounts){
		discounts.setImgsrc("images/" + discounts.getImgsrc());
		discounts.setShopImgSrc("images/" + discounts.getShopImgSrc());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			discounts.setBegintime(sdf.format(sdf.parse(discounts.getBegintime())));
			discounts.setEndtime(sdf.format(sdf.parse(discounts.getEndtime())));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 显示优惠商品信息
	 * @return
	 */
	public String queryDiscounts(){
		Map<String, String> paramsM = new HashMap<String, String>();
		try {
			paramsM.put("id", id);
			discounts = discountsService.queryDiscounts(paramsM);
		} catch (Exception e) {
			e.printStackTrace();
		}
		convertData(discounts);
		return SUCCESS;
	}
	
	/**
	 * 根据查询条件查询的总数
	 * @param paramsM
	 */
	private void queryDatasCount(Map<String, Object> paramsM) {
		if(data == null){
			data = new HashMap<String, Object>();
		}
		data.put("count", discountsService.queryCountByCondition(paramsM));
	}
	
	public String queryListDiscounts(){
		Map<String, Object> paramsM = new HashMap<String, Object>();
		try {
			if(start <= 0){
				start = 0;
			}
			if(limit <= 0){
				limit = 20;
			}
			if(discounts != null){
				if(!StringTools.isEmpty(discounts.getType())){
					paramsM.put("type", discounts.getType());
				}
				if(!StringTools.isEmpty(discounts.getShopName())){
					paramsM.put("shopName", discounts.getShopName());
				}else{
					paramsM.put("shopName", null);
				}
			}
			paramsM.put("start", start);
			paramsM.put("limit", limit);
			discountList = discountsService.queryListDiscounts(paramsM);
			queryDatasCount(paramsM);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "list";
	}
	
	/**
	 * 删除单个商品的方法
	 * 
	 * @return
	 */
	public String deleteDiscounts(){
		String id = discounts.getId();
		if(!StringTools.isEmpty(id)){
			Map<String, Object> paramsM = new HashMap<String, Object>();
			paramsM.put("id", id);
			discountsService.deleteDiscounts(paramsM);
		}
		return queryListDiscounts();
	}
	
	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getDataJson() {
		return dataJson;
	}

	public void setDataJson(String dataJson) {
		this.dataJson = dataJson;
	}

	public Discounts getDiscounts() {
		return discounts;
	}

	public void setDiscounts(Discounts discounts) {
		this.discounts = discounts;
	}

	public IDiscountsService getDiscountsService() {
		return discountsService;
	}

	public void setDiscountsService(IDiscountsService discountsService) {
		this.discountsService = discountsService;
	}

	public List<Discounts> getDiscountList() {
		return discountList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public File getShopImgFile() {
		return shopImgFile;
	}

	public void setShopImgFile(File shopImgFile) {
		this.shopImgFile = shopImgFile;
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}
	
}
